﻿using System;
using System.Collections.Generic;
using System.Text;
using Tunnel_Cross_Section.相关类;
using System.Data;
using System.Windows.Forms;

namespace Tunnel_Cross_Section.DeviceConfig
{
    public class DeviceConfigHelper
    {
        private DataBase md;
        public DeviceConfigHelper(DataBase m)
        {
            this.md = m;
        }

        public double OnDeviceSelected(string no)
        {
            Calibrate c = new Calibrate(md, no, DateTime.Now);
            return c.Cvalue;
        }

        public List<string> GetDeviceNames()
        {
            List<string> names = new List<string>();
            string sql = "select [DeviceNo] from [Calibration] group by [DeviceNo]";
            DataTable dt = md.GetDataSet(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                names.Add(dt.Rows[i][0].ToString());
            }
            return names;
        }

        public void UpdateDeviceValue(string Dno,double val)
        {
            string sql = "update [Calibration] set [Calibrate] =" + val + " where [DeviceNo]='"
                + Dno + "'";
            md.ExecuteSQL(sql);
        }

        public void UpdateDeviceNo(string oldNo, string newNo)
        {
            string sql = "update [Calibration] set [DeviceNo] ='" + newNo + "' where [DeviceNo]='"
                         + oldNo + "'";
            md.ExecuteSQL(sql);
        }

        public void AddDeivce(string no, double val)
        {
            string sql = "select [Calibrate] from [Calibration] where [DeviceNo]='"
                              + no + "'";
            DataTable dt = md.GetDataSet(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("设备‘" + no + "’已存在！");
                return;
            }
            else
            {
                sql = "insert into [Calibration] values('" + no + "',"
                    + val + ",'" + DateTime.Now.ToString() + "','','')";
                md.ExecuteSQL(sql);
            }
        }
    }
}
